package com.wawei.seeds;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.igexin.sdk.PushManager;
import com.wawei.seeds.model.HeroInfo;
import com.wawei.seeds.services.WalnutPortraitServiceHandler;
import com.wawei.seeds.services.WalnutRegAuthServiceHandler;
import com.wawei.seeds.utils.PrefUtils;
import com.wawei.seeds.utils.RetrofitNetworkService;
import com.wawei.seeds.utils.WalnutBaseNofityHandler;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit.RestAdapter;

/**
 *
 * Created by t27891 on 15/7/4.
 */
public class WalnutRegistrationActivity extends ActionBarActivity {
    @InjectView(R.id.walnut_event_name)TextView wndTitle;
    @InjectView(R.id.walnut_event_sharing)View sharingView;
    @InjectView(R.id.walnut_logo)View backView;
    @InjectView(R.id.walnut_reg_submit)View submitView;

    @InjectView(R.id.walnut_reg_staffno)EditText staffnoView;
    @InjectView(R.id.walnut_reg_mobile)EditText mobileView;
    @InjectView(R.id.walnut_reg_alias)EditText aliasView;
    @InjectView(R.id.walnut_reg_password)EditText passwordView;

    private RegistrationActivityNotifyHandler notifyHandler;
    private WalnutRegAuthServiceHandler serviceHandler;

    public static final int REG_HERO = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_walnut_reg);
        ButterKnife.inject(this);

        notifyHandler = new RegistrationActivityNotifyHandler(this);
        serviceHandler = new WalnutRegAuthServiceHandler();

        init();
    }

    private void init(){
        wndTitle.setText("");
        sharingView.setVisibility(View.GONE);

        backView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WalnutRegistrationActivity.this.finish();
            }
        });

        submitView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submit();
            }
        });
    }

    private void submit(){
        HeroInfo heroInfo = new HeroInfo();
        heroInfo.userName = staffnoView.getText().toString();
        heroInfo.mobile = mobileView.getText().toString();
        heroInfo.email = staffnoView.getText().toString();
        heroInfo.alias = aliasView.getText().toString();
        heroInfo.password = passwordView.getText().toString();

        if (heroInfo.mobile == null || heroInfo.mobile.length() != 11) {
            Toast.makeText(this, "手机号码必须是11位的数字!", Toast.LENGTH_LONG).show();
            return;
        }

        RestAdapter restAdapter = RetrofitNetworkService.makeAsyncRestAdapter();
        String clientId = PushManager.getInstance().getClientid(this);
        serviceHandler.register(restAdapter, heroInfo, clientId, notifyHandler, REG_HERO);
    }

    @Override
    protected void onDestroy() {
        notifyHandler = null;
        serviceHandler = null;
        super.onDestroy();
    }

    private static class RegistrationActivityNotifyHandler extends WalnutBaseNofityHandler<WalnutRegistrationActivity, HeroInfo>{
        public RegistrationActivityNotifyHandler(WalnutRegistrationActivity walnutRegistrationActivity) {
            super(walnutRegistrationActivity);
        }

        @Override
        public void handleOtherMessage(Message msg) {
            if(msg.what == REG_HERO){
                Bundle bundle = msg.getData();
                if (bundle == null) {
                    Toast.makeText(get(), get().getString(R.string.walnut_register_fail), Toast.LENGTH_LONG).show();
                    return;
                }

                HeroInfo heroInfo = (HeroInfo)bundle.getSerializable(RetrofitNetworkService.RESULT_DATA);

                if (heroInfo == null) {
                    Toast.makeText(get(), get().getString(R.string.walnut_register_fail), Toast.LENGTH_LONG).show();
                    return;
                }

                SeedsApp app = (SeedsApp)get().getApplication();
                app.getSession().put(SeedsApp.LOGIN_USER, heroInfo);

                PrefUtils.markUserPassword(get(), heroInfo.userName, heroInfo.password);
                PrefUtils.markUserId(get(), Integer.valueOf(heroInfo.userId == null ? "0" : heroInfo.userId));
                PrefUtils.markUserProfileImgUri(get(), heroInfo.headPortrait);

                Intent mainIntent = new Intent(get(), WalnutMainActivity.class);
                get().startActivity(mainIntent);
                get().finish();
            }
        }
    }
}
